package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenInfoEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * recommend 里小编推荐模块
 * Created by WangQing on 2016/12/9.
 */

public class EditorRecommendAdapter extends RecyclerView.Adapter<EditorRecommendAdapter.ViewHolde> implements View.OnClickListener {
    private Context context;
    private List<ListenInfoEntity> list = new ArrayList<>();

    public EditorRecommendAdapter(Context context, List<ListenInfoEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolde onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_item,parent,false);
        ViewHolde holde = new ViewHolde(view);
        return holde;
    }

    @Override
    public void onBindViewHolder(ViewHolde holder, int position) {
        if (list.get(position).getTrackTitle()!=null) {
            holder.title.setText(list.get(position).getTrackTitle());
        }
        if (list.get(position).getTitle()!=null) {
            holder.author.setText(list.get(position).getTitle());
        }
        if (list.get(position).getCoverLarge()!=null) {
            Picasso.with(context).load(list.get(position).getCoverLarge()).into(holder.img);

        }
        holder.layout.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);

        Toast.makeText(context,"点击第"+position+"个",Toast.LENGTH_SHORT).show();

    }

    public static class ViewHolde extends RecyclerView.ViewHolder{

        private final ImageView img;
        private final TextView title;
        private final TextView author;
        private LinearLayout layout;

        public ViewHolde(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.recommend_item_img);
            title = (TextView) itemView.findViewById(R.id.recommend_item_title);
            author = (TextView) itemView.findViewById(R.id.recommend_item_author);
            layout = (LinearLayout) itemView.findViewById(R.id.recommend_item_layout);
        }
    }
}
